from rocketmq.client import Producer, Message
import json


# 发送数据到消息队列
def send_data(message_content,topic="henan_rsanalysis_s2_1c_collect",addr='192.168.1.70:9876'):
    producer = Producer("data")
    producer.set_namesrv_addr(addr)
    producer.start()
    message_body = json.dumps(message_content)
    msg = Message(topic)
    msg.set_tags("TagA")
    msg.set_body(message_body)
    _ = producer.send_sync(msg)
    producer.shutdown()
    return 


def core(topic,addr,message_queue):
    
    while True:
        element = message_queue.get()
        if element is None: break
        
        message_content = {
            "tile": row[1],  # 假设其他属性字段在结果中的索引为1
            "product_date": row[2].strftime("%Y-%m-%d"),
            "cpjbdm": "1",
            "cpjdmc": "L1C",
            "imgtype_code": "S2",
            "imgtype_name": "Sentinel-2",
            "cloud": float(row[4]),
            "tif_path": row[3],
            "geometry": row[0]}
        send_data(message_content,topic,addr)
 
    return


# 发送数据到消息队列
def send_data_(data):
    topic = "henan_rsanalysis_s2_1c_collect"
    producer = Producer("data")
    producer.set_namesrv_addr('192.168.1.70:9876')
    producer.start()
    for row in tqdm(data,desc="发送数据"):
        message_content = {
            "tile": row[1],  # 假设其他属性字段在结果中的索引为1
            "product_date": row[2].strftime("%Y-%m-%d"),
            "cpjbdm": "1",
            "cpjdmc": "L1C",
            "imgtype_code": "S2",
            "imgtype_name": "Sentinel-2",
            "cloud": float(row[4]),
            "tif_path": row[3],
            "geometry": row[0]}
        message_body = json.dumps(message_content)
        msg = Message(topic)
        msg.set_tags("TagA")
        msg.set_body(message_body)
        _ = producer.send_sync(msg)
    producer.shutdown()
    return 